//
//  CreatCardViewController.m
//  jiyujiaProgram
//
//  Created by NiKi on 15/10/31.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "CreatCardViewController.h"
#import "AddImageViewController.h"

@interface CreatCardViewController () <UITextFieldDelegate,UITextViewDelegate,UIAlertViewDelegate>

@end

@implementation CreatCardViewController
{
    UITextField *_titleText;
    UITextView *_contentText;
    UILabel *_placeLabel;
    UIImageView *_cardImg;
    NSString *_imgId;
    
    MemberModel *_memberModel;
    TeacherModel *_teacherModel;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"发起帖子"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    
    [self creatView];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self loadUserInfo];
}

- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)creatView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 69, ECSCREEN_W, 44)];
    titleView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [self.view addSubview:titleView];
    
    _titleText = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 44)];
    _titleText.delegate = self;
    _titleText.font = [UIFont systemFontOfSize:13.f];
    _titleText.placeholder = @"标题";
    [titleView addSubview:_titleText];
    
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 118, ECSCREEN_W, 100)];
    contentView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [self.view addSubview:contentView];
    
    
    _contentText = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 100)];
    _contentText.delegate = self;
    [contentView addSubview:_contentText];
    
    
    _placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 30)];
    _placeLabel.text = @"输入此刻内容...";
    _placeLabel.textColor = [UIColor lightGrayColor];
    _placeLabel.font = [UIFont systemFontOfSize:13.f];
    [contentView addSubview:_placeLabel];
    
    
    UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 223, ECSCREEN_W, 100)];
    photoView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [self.view addSubview:photoView];
    
    
    _cardImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    _cardImg.image = ECIMAGENAME(@"creatcard_add.png");
    [photoView addSubview:_cardImg];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 80, 80);
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [photoView addSubview:button];
    
    
    
    
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    submitButton.frame = CGRectMake(10, ECSCREEN_H - 50, ECSCREEN_W - 20, 40);
    submitButton.backgroundColor = NAVC_COLOR;
    submitButton.clipsToBounds = YES;
    submitButton.layer.cornerRadius = 5;
    [submitButton setTitle:@"确认发布" forState:UIControlStateNormal];
    [submitButton setTitleColor:ECCOLOR(255, 255, 255, 1) forState:UIControlStateNormal];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [submitButton addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitButton];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_titleText resignFirstResponder];
    [_contentText resignFirstResponder];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _contentText.userInteractionEnabled = NO;
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _contentText.userInteractionEnabled = YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (void)textViewDidBeginEditing:(UITextView *)textView
{
    _placeLabel.hidden = YES;
    _titleText.userInteractionEnabled = NO;
}


- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeLabel.hidden = NO;
    }
    _titleText.userInteractionEnabled = YES;
}


- (void)btnClick:(UIButton *)button
{
    NSLog(@"选相册照片");
    AddImageViewController *vc = [[AddImageViewController alloc] init];
    [vc setAddImage:^(NSString *imgID, NSString *imgURL,NSData *imgData) {
        _cardImg.image = [UIImage imageWithData:imgData];
        _imgId = imgID;
    }];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)submitClick:(UIButton *)button
{
    NSLog(@"确认发布");
    
    
    if (_titleText.text.length == 0) {
        SHOW_ALERT(@"提示", @"请填写帖子名", @"好的", nil, nil);
    }else if (_contentText.text.length == 0){
        SHOW_ALERT(@"提示", @"请填写帖子内容", @"好的", nil, nil);
    }else{
        
        if (_imgId.length == 0) {
            _imgId = @"";
        }
        
        NSDictionary *data = @{
                               @"circle_id":self.circleId,
                               @"title":_titleText.text,
                               @"content":_contentText.text,
                               @"imgIds":_imgId,
                               @"member_id":_memberModel.member_Id,
                               };
        
        NSDictionary *param = @{
                                @"data":data,
                                };
        
        [[HttpManager defaultManager] postRequestToUrl:RequestURL(ADDCARD) params:param complete:^(BOOL successed, NSDictionary *result) {
            if (successed) {
                if ([result[@"errCode"] integerValue] == 200) {
                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, self);
                }else{
                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, nil);
                }
            }
        }];
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
